package com.tj.bo.trade.server.dao.impl;

import com.fqgj.common.api.Page;
import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.datasource.DataSource;
import com.tj.bo.trade.server.dao.BinaryTradeDao;
import com.tj.bo.trade.server.entity.BinaryTradeEntity;
import com.tj.bo.trade.server.entity.bussiness.TradeDailyAvgEntity;
import com.tj.bo.trade.server.entity.bussiness.TradeDailyWinRateEntity;
import com.tj.bo.trade.server.entity.bussiness.TradeDispersionEntity;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: binary
 * Date: 2020-06-05
 * Time: 11:03:09.754
 */

@Repository
@DataSource("dataSourceTrade")
public class BinaryTradeDaoImpl extends AbstractBaseMapper<BinaryTradeEntity> implements BinaryTradeDao {

    private List<BinaryTradeEntity> selectsBinaryTradeListByParams(Map<String, Object> params) {
        if (!params.containsKey("deleted")) {
            params.put("deleted", 0);
        }
        return getSqlSession().selectList("selectsBinaryTradeListByParams", params);
    }

    @Override
    public BinaryTradeEntity selectBinaryTradeByTradeNo(String tradeNo) {
        Map param = new HashMap<>();
        param.put("tradeNo", tradeNo);
        return getSqlSession().selectOne("selectBinaryTradeByTradeNo", param);
    }

    @Override
    public List<BinaryTradeEntity> selectBinaryTradeListByPageCondition(Page page, Integer tradeType, String userCode) {
        Map param = new HashMap();
        param.put("page", page);
        param.put("tradeType", tradeType);
        param.put("userCode", userCode);
        page.setTotalCount(getSqlSession().selectOne("countBinaryTrade", param));
        return getSqlSession().selectList("selectBinaryTradeListByPageCondition", param);
    }

    @Override
    public List<BinaryTradeEntity> selectsBinaryTradeListByCondition(Integer orderStatus) {
        Map param = new HashMap();
        param.put("orderStatus", orderStatus);
        return getSqlSession().selectList("selectsBinaryTradeListByCondition", param);
    }

    @Override
    public List<BinaryTradeEntity> selectsBinaryTradeListByStatusAndTime(Integer orderStatus, Integer tradeType, Date endTime) {
        Map param = new HashMap();
        param.put("orderStatus", orderStatus);
        param.put("tradeType", tradeType);
        param.put("endTime", endTime);
        return getSqlSession().selectList("selectsBinaryTradeListByStatusAndTime", param);
    }

    @Override
    public List<BinaryTradeEntity> selectsBinaryTradeListByTime(String userCode, String productCode, Integer orderStatus, Integer tradeType, Date startDate, Date endDate) {
        Map<String, Object> params = new HashMap<>();
        params.put("userCode", userCode);
        params.put("orderStatus", orderStatus);
        params.put("tradeType", tradeType);
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("orderBy", " id asc ");
        return selectsBinaryTradeListByParams(params);
    }

    @Override
    public Integer countBinaryTrade(String userCode, Integer orderStatus, Integer tradeType) {
        Map param = new HashMap();
        param.put("orderStatus", orderStatus);
        param.put("userCode", userCode);
        param.put("tradeType", tradeType);
        return getSqlSession().selectOne("countBinaryTrade", param);
    }

    @Override
    public Integer countBinaryTradeByTime(String productCode, Date queryTime) {
        Map param = new HashMap();
        param.put("productCode", productCode);
        param.put("queryTime", queryTime);
        return getSqlSession().selectOne("countBinaryTradeByTime", param);
    }

    @Override
    public List<BinaryTradeEntity> selectBinaryTradeListByUserCodeAndProductCode(String userCode, Integer tradeType, Integer orderStatus, String productCode) {
        Map param = new HashMap();
        param.put("userCode", userCode);
        param.put("tradeType", tradeType);
        param.put("productCode", productCode);
        param.put("orderStatus", orderStatus);
        return getSqlSession().selectList("selectBinaryTradeListByUserCodeAndProductCode", param);
    }

    @Override
    public BigDecimal calculateTotalProfitAmount(Date createDate) {
        Map param = new HashMap();
        param.put("createDate", createDate);
        return getSqlSession().selectOne("calculateTotalProfitAmount", param);
    }


    @Override
    public BigDecimal calculateUserTotalProfitAmount(Date createDate, String userCode) {
        Map param = new HashMap();
        param.put("createDate", createDate);
        param.put("userCode", userCode);
        return getSqlSession().selectOne("calculateUserTotalProfitAmount", param);
    }

    @Override
    public boolean isProductNewUser(String userCode, String productCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("userCode", userCode);
        param.put("productCode", productCode);
        Integer num = getSqlSession().selectOne("isProductNewUser", param);
        if (num == null || num == 0) {
            return true;
        }
        return false;
    }

    @Override
    public TradeDailyAvgEntity selectTradeDailyAvg(String userCode, String productCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("userCode", userCode);
        params.put("productCode", productCode);
        return getSqlSession().selectOne("selectTradeDailyAvg", params);
    }

    @Override
    public TradeDispersionEntity selectTradeDispersion(String userCode, String productCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("userCode", userCode);
        params.put("productCode", productCode);
        return getSqlSession().selectOne("selectTradeDispersion", params);
    }

    @Override
    public List<TradeDailyWinRateEntity> selectDailyWinRate(String userCode, String productCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("userCode", userCode);
        params.put("productCode", productCode);
        return getSqlSession().selectList("selectDailyWinRate", params);
    }
}